package cn.itrip.biz.service.itripComment;
import cn.itrip.beans.pojo.ItripComment;
import cn.itrip.beans.pojo.ItripHotelOrder;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.vo.comment.ItripListCommentVO;
import cn.itrip.biz.service.itripComment.ItripCommentService;
import cn.itrip.dao.itripComment.ItripCommentMapper;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.dao.itripHotelOrder.ItripHotelOrderMapper;
import cn.itrip.dao.itripImage.ItripImageMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import cn.itrip.common.Constants;
import org.springframework.web.bind.annotation.RequestBody;

@Service
public class ItripCommentServiceImpl implements ItripCommentService {
    @Resource
    private ItripCommentMapper itripCommentMapper;
    @Resource
    private ItripImageMapper itripImageMapper;
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;


    public ItripComment getItripCommentById(Long id)throws Exception{
        return itripCommentMapper.getItripCommentById(id);
    }

    public List<ItripComment>	getItripCommentListByMap(Map<String,Object> param)throws Exception{
        return itripCommentMapper.getItripCommentListByMap(param);
    }

    public Integer getItripCommentCountByMap(Map<String,Object> param)throws Exception{
        return itripCommentMapper.getItripCommentCountByMap(param);
    }

    public Integer itriptxAddItripComment(ItripComment itripComment)throws Exception{
            itripComment.setCreationDate(new Date());
            return itripCommentMapper.insertItripComment(itripComment);
    }

    @Override
    public void itriptxAddItripComment(ItripComment itripComment, List<ItripImage> imageList) throws Exception {
        //计算综合得分 = （设施 + 卫生 + 服务 + 位置） / 4
        int sum = itripComment.getFacilitiesScore() + itripComment.getHygieneScore()
                + itripComment.getPositionScore() + itripComment.getServiceScore();
        itripComment.setScore(sum / 4);
        //保存评论
        itripCommentMapper.insertItripComment(itripComment);
        Long commentId = itripComment.getId();
        //保存图片
        if(EmptyUtils.isNotEmpty(imageList)){
            for(ItripImage image:imageList){
                image.setTargetId(commentId);
                itripImageMapper.insertItripImage(image);
            }
        }
        //订单状态要变更为已评论
        ItripHotelOrder order = new ItripHotelOrder();
        order.setId(itripComment.getOrderId());
        order.setOrderStatus(4);
        order.setModifyDate(itripComment.getCreationDate());
        order.setModifiedBy(itripComment.getCreatedBy());
        itripHotelOrderMapper.updateItripHotelOrder(order);
    }

    public Integer itriptxModifyItripComment(ItripComment itripComment)throws Exception{
        itripComment.setModifyDate(new Date());
        return itripCommentMapper.updateItripComment(itripComment);
    }

    public Integer itriptxDeleteItripCommentById(Long id)throws Exception{
        return itripCommentMapper.deleteItripCommentById(id);
    }

    /*public Page<ItripComment> queryItripCommentPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize)throws Exception{
        Integer total = itripCommentMapper.getItripCommentCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripComment> itripCommentList = itripCommentMapper.getItripCommentListByMap(param);
        page.setRows(itripCommentList);
        return page;
    }*/

    public Page<ItripListCommentVO> queryItripCommentPageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = itripCommentMapper.getItripCommentCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<ItripListCommentVO> itripCommentList = itripCommentMapper.getItripCommentListByMapWithDetail(param);
        page.setRows(itripCommentList);
        return page;
    }

}
